using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;

namespace CustomActions
{
	[RunInstaller(true)]
	public partial class AddInFile : Installer
	{
		public AddInFile()
		{
			InitializeComponent();
		}

		public override void Install(IDictionary stateSaver)
		{
			base.Install(stateSaver);

            Context.LogMessage("-------------------- AddInFile installer beginning --------------------");

            try
            {
                // Read in template configuration file
                string cfgFileName = Path.Combine(Context.Parameters["targetdir"], "DoxyComment.addin");
                Context.LogMessage("Loading configuration template: " + cfgFileName);
                string cfgFile = File.ReadAllText(cfgFileName);

                // Replace installation path in the configuration
                string addInPath = Path.Combine(Context.Parameters["targetdir"], "DoxyComment.dll");
                Context.LogMessage("Setting add-in full path: " + addInPath);
                Regex regEx = new Regex("FULL_ADDIN_PATH");
                string modCfgFile = regEx.Replace(cfgFile, addInPath);

                // Write modified configuration back to file
                Context.LogMessage("Writing config file back to disk.");
                File.WriteAllText(cfgFileName, modCfgFile);
            }
            catch (Exception excp)
            {
                Context.LogMessage("Unsuccessful:");
                Context.LogMessage(excp.Message);
                Context.LogMessage(excp.StackTrace);
            }

            Context.LogMessage("-------------------- AddInFile installer ending --------------------");
        }

		public override void Commit(IDictionary savedState)
		{
			base.Commit(savedState);
		}

		public override void Rollback(IDictionary savedState)
		{
			base.Rollback(savedState);
		}

		public override void Uninstall(IDictionary savedState)
		{
			base.Uninstall(savedState);

            Context.LogMessage("-------------------- AddInFile uninstaller beginning --------------------");

            Context.LogMessage("-------------------- AddInFile uninstaller ending --------------------");
        }
	}
}